using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.Services.Protocols;
using System.Xml;
using System.Drawing;
using MSU_UserControls.MSUWS;


namespace MSU_UserControls
{
    [System.ComponentModel.Description("MSU Login")]
    public partial class MSULogin : MSU_UserControl, SmartPart.IConnectionProviderControl
    {
        #region IConnectionProviderControl Members

        public object GetProviderData()
        {
            if (_msu_User == null)
                return false;
            else
                return true;
        }

        public string ProviderMenuLabel
        {
            get { return "Sends text data to"; }
        }

        #endregion

        #region Events(Page_Load, btnLogin_Click, btnLogOut_Click)

        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPControl.GetContextWeb(this.Context);
            SPUser currentUser = web.CurrentUser;
            if (currentUser != null)
            {
                txtLoginUserName.Visible = false;
                lblLoginUserName.Visible = false;
                txtLoginUserName.Text = "";

                string usuario = currentUser.LoginName.ToString().Substring(currentUser.LoginName.ToString().IndexOf(@"\") + 1).ToString();
                lblNomeUsuario.Text = usuario;
                lblNomeUsuarioNaoLogado.Text = "Username: " + usuario;


                if (_msu_User == null)
                    ShowLoginControl(true);
                else
                    ShowLoginControl(false);
            }
            else
            {
                if (_msu_User == null)
                {
                    ShowLoginControl(true);
                    txtLoginUserName.Visible = true;
                    lblLoginUserName.Visible = true;
                }
                else
                {
                    ShowLoginControl(false);
                    txtLoginUserName.Visible = false;
                    lblLoginUserName.Visible = false;
                }

                lblNomeUsuario.Text = txtLoginUserName.Text;
                lblNomeUsuarioNaoLogado.Text = "Signed Out.";
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {                
                string username;
                if (txtLoginUserName.Visible)
                    username = txtLoginUserName.Text;
                else
                    username = lblNomeUsuario.Text;

                _msu_User = WSMSU.UserLogin(_msu_Adm, username, txtLoginPassword.Text, false);
                
                lblResult.Text = "";
                txtLoginUserName.Visible = false;
                lblLoginUserName.Visible = false;
                ShowLoginControl(false);
            }
            catch (SoapException soapEx)
            {
                //Load the Detail element of the SoaopException object
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(soapEx.Detail.OuterXml);

                XmlNamespaceManager nsManager = new
                XmlNamespaceManager(doc.NameTable);

                // Add the namespace to the NamespaceManager
                nsManager.AddNamespace("errorNS", "http://tempuri.org/MSUWS");

                XmlNode categoryNode = doc.DocumentElement.SelectSingleNode("errorNS:Error", nsManager);
                string errorTypeNumber = categoryNode.SelectSingleNode("errorNS:ErrorTypeNumber", nsManager).InnerText;
                string message = categoryNode.SelectSingleNode("errorNS:Message", nsManager).InnerText;
                string stackTrace = categoryNode.SelectSingleNode("errorNS:StackTrace", nsManager).InnerText;
                string innerError = categoryNode.SelectSingleNode("errorNS:InnerError", nsManager).InnerText;
                string onScreen = categoryNode.SelectSingleNode("errorNS:OnScreen", nsManager).InnerText;

                if (string.Compare(onScreen, true.ToString()) == 0)
                {
                    lblResult.Text += message + "\r\n";
                    lblResult.ForeColor = Color.DarkRed;
                }
                else
                {
                    lblResult.Text += "Unexpected error. Details:\r\n";
                    lblResult.Text += "Soap fault code: " + soapEx.Code.ToString() + "\r\n";
                    lblResult.Text += "ErrorType Number is: " + errorTypeNumber + "\r\n";
                    lblResult.Text += "Error Message is: " + message + "\r\n";
                    lblResult.Text += "Error StackTrace is: " + stackTrace + "\r\n";
                    lblResult.Text += "Error InnerError is: " + (string.IsNullOrEmpty(innerError) ? "null" : innerError) + "\r\n";
                    lblResult.ForeColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                lblResult.Text += "Unexpected error. Details:\r\n";
                lblResult.Text += "Error Message is: " + ex.Message + "\r\n";
                lblResult.Text += "Error StackTrace is: " + ex.StackTrace + "\r\n";
                lblResult.Text += "Error InnerException is: " + ((ex.InnerException == null) ? "None" : ex.InnerException.Message) + "\r\n";
                lblResult.ForeColor = Color.Red;
            }
        }
        
        protected void btnLogOut_Click(object sender, EventArgs e)
        {
            
            SPWeb web = SPControl.GetContextWeb(this.Context);
            SPUser currentUser = web.CurrentUser;
            
            _msu_User = null;
            ShowLoginControl(true);

            if (currentUser != null)
            {
                txtLoginUserName.Visible = false;
                lblLoginUserName.Visible = false;
            }
            else
            {
                txtLoginUserName.Visible = true;
                lblLoginUserName.Visible = true;
            }         

        } 

        #endregion

        #region Methods(ShowLoginControl)

        public void ShowLoginControl(bool show)
        {
            if (show)
            {
                btnLogOut.Visible = false;
                lblWelcome.Visible = false;
                lblNomeUsuario.Visible = false;
                lblNomeUsuarioNaoLogado.Visible = true;
                lblLoginPassword.Visible = true;
                txtLoginPassword.Visible = true;
                btnLogin.Visible = true;                
            }
            else
            {
                lblNomeUsuarioNaoLogado.Visible = false;
                btnLogOut.Visible = true;
                lblWelcome.Visible = true;
                lblNomeUsuario.Visible = true;
                lblLoginPassword.Visible = false;
                txtLoginPassword.Visible = false;
                btnLogin.Visible = false;
            }
        }
        
        #endregion
    }
}